package org.sith.taskrunner.ejb;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TasksManager {

	private static TasksManager instance;

	private Map<String, String> tasksMap = new HashMap<String, String>();

	private TasksManager() {
	}

	public static TasksManager getInstance() {
		if (instance == null) {
			instance = new TasksManager();
		}
		return instance;
	}

	public boolean addTask(String taskName, String taskLink) {
		if (!existsTask(taskName)) {
			tasksMap.put(taskName, taskLink);
			return true;
		} else
			return false;
	}

	public boolean deleteTask(String taskName) {
		boolean sucksessfullRemove = false;
		if (existsTask(taskName)) {
			String removeValueAfter = tasksMap.get(taskName);
			String removeValueBefore = tasksMap.remove(taskName);
				sucksessfullRemove = true;
		}
		return sucksessfullRemove;
	}

	public boolean existsTask(String taskName) {
		boolean exists = false;
		for (Map.Entry<String, String> entry : tasksMap.entrySet()) {
			String task = entry.getKey();
			if (task.toLowerCase().equals(taskName.toLowerCase())) {
				exists = true;
			}
		}
		return exists;
	}

	public String getLink(String taskName) {
		return tasksMap.get(taskName);
	}
	public URL getTaskAsURL(String taskName){
		return null;
	}
	
	@Override
	public String toString() {
		return "TasksList=" + tasksMap;
	}

	public Set<Entry<String, String>> entrySet(){
		return tasksMap.entrySet();
	}

    public void clear(){
        tasksMap.clear();
    }
    
    public int getSize(){
    	return tasksMap.size();
    }

}
